package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.Patient;
import com.cqu.hospitalsystem.entity.Registration;
import com.cqu.hospitalsystem.service.PatientService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("patient")
@Tag(name = "PatientController", description = "患者管理")
public class PatientController {
    /**
     * 服务对象
     */
    @Resource
    private PatientService patientService;

    @GetMapping
    @Operation(summary = "分页查询患者", description = "根据筛选条件和分页信息查询患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Patient>> queryByPage(Patient patient, PageRequest pageRequest) {
        return ResponseEntity.ok(this.patientService.queryByPage(patient, pageRequest));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID查询患者", description = "根据患者ID查询单个患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "患者不存在")
    })
    public ResponseEntity<Patient> queryById(@PathVariable("id") @Parameter(description = "患者ID") Long id) {
        return ResponseEntity.ok(this.patientService.queryById(id));
    }

    @PostMapping
    @Operation(summary = "新增患者", description = "创建新的患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Patient> add(@RequestBody @Parameter(description = "患者对象") Patient patient) {
        return ResponseEntity.ok(this.patientService.insert(patient));
    }

    @PutMapping
    @Operation(summary = "编辑患者", description = "更新患者信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Patient> edit(@RequestBody @Parameter(description = "患者对象") Patient patient) {
        return ResponseEntity.ok(this.patientService.update(patient));
    }

    @DeleteMapping
    @Operation(summary = "删除患者", description = "根据患者ID删除患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "患者ID") Long id) {
        return ResponseEntity.ok(this.patientService.deleteById(id));
    }

    @PostMapping("loginPatient")
    @Operation(summary = "患者登录", description = "根据患者信息进行登录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "400", description = "登录失败")
    })
    public DataResult loginPatient(@RequestBody @Parameter(description = "患者登录信息") Patient patient) {
        Patient loginPatient = this.patientService.loginPatient(patient);
        if (loginPatient != null) {
            return DataResult.successByData(loginPatient);
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping(value = "editInfo")
    @Operation(summary = "修改患者基本信息", description = "根据患者ID修改患者基本信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "修改失败")
    })
    public DataResult editInfo(@RequestBody @Parameter(description = "患者对象") Patient patient) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.patientService.editInfo(patient));
        return DataResult.successByData(jsonObject);
    }

    @PostMapping(value = "editPwd")
    @Operation(summary = "修改患者密码", description = "根据患者ID和旧密码修改患者密码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "修改失败")
    })
    public DataResult editPwd(@RequestBody @Parameter(description = "密码修改参数") Map<String,Object> param) {
        String patientPwd = param.get("patientPwd").toString();
        String newPwd = param.get("newPwd").toString();
        long patientId = Integer.parseInt(param.get("patientId").toString());
        JSONObject jsonObject = new JSONObject();
        Patient patient = this.patientService.searchPwd(patientId);
        if (patient.getPatientPassword().equals(patientPwd)) {
            jsonObject.put("isSuccess", this.patientService.editPwd(patientId, newPwd));
            jsonObject.put("error", "修改失败");
        } else {
            jsonObject.put("isSuccess", false);
            jsonObject.put("error", "旧密码错误");
        }
        return DataResult.successByData(jsonObject);
    }

    @PostMapping("register")
    @Operation(summary = "患者注册", description = "创建新的患者注册信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "注册失败")
    })
    public DataResult register(@RequestBody @Parameter(description = "患者注册信息") Patient patient) {
        Patient dataBasePatient = this.patientService.searchForPatient(patient);
        if (dataBasePatient == null) {
            return DataResult.successByData(this.patientService.insert(patient));
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_REPET);
        }
    }

    @PostMapping("doctor_visit")
    @Operation(summary = "医生访问患者信息", description = "根据注册ID查询患者信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "患者不存在")
    })
    public DataResult doctor_visit(@RequestBody @Parameter(description = "注册信息") Registration registration) {
        Patient regPatient = this.patientService.reg_patient(registration.getRegId());
        if (regPatient != null) {
            return DataResult.successByData(regPatient);
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping("searchPatient")
    @Operation(summary = "搜索患者", description = "根据患者信息搜索患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "患者不存在")
    })
    public DataResult searchPatient(@RequestBody @Parameter(description = "患者搜索信息") Patient patient) {
        Patient res = this.patientService.searchPatient(patient);
        if (res != null) {
            return DataResult.successByData(res);
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping("regByReception")
    @Operation(summary = "接待处注册患者", description = "根据患者信息在接待处注册患者")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "注册失败")
    })
    public DataResult regByReception(@RequestBody @Parameter(description = "患者注册信息") Patient patient) {
        return this.patientService.regByReception(patient);
    }

    @PostMapping("getHistoryReg")
    public DataResult getHistoryReg(@RequestBody Patient patient){
        return this.patientService.getHistoryReg(patient.getPatientId());
    }
}